package org.mclll.xwangplugin.town.command.executor;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.mclll.xwangplugin.town.database.domain.TownPlayerDO;
import org.mclll.xwangplugin.town.database.mapper.manage.TownManage;
import org.mclll.xwangplugin.town.database.mapper.manage.TownPlayerManage;
import org.mclll.xwangplugin.town.enums.PlayerTownIdentity;
import org.mclll.xwangplugin.town.lp.LuckPermsTown;
import org.mclll.xwangplugin.util.PlayerTools;

import java.util.Collections;
import java.util.List;

import static org.mclll.xwangplugin.town.config.TownContext.*;

/**
 * @author: geduo
 * @date: 2023/9/23 19:32
 */
public class KickPlayerExecutor {

    public static void kickPlayer(CommandSender sender, String[] args) {
        // poitown kick <玩家名> [confirm]
        if (args.length < 2) {
            sender.sendMessage(KICK_PLAYER_COMMAND_FORMAT);
            return;
        }
        if (args.length == 2) {
            // 需要二次确认踢出玩家
            sender.sendMessage(
                    Component.text(String.format(KICK_PLAYER_CONFIRM, args[1]))
                            .appendNewline()
                            .append(
                                    Component.text(CONFIRM_BUTTON)
                                            .clickEvent(ClickEvent.runCommand(String.format("/poitown kick %s confirm", args[1])))
                                            .hoverEvent(HoverEvent.showText(Component.text(CONFIRM_BUTTON)))
                            )
                            .append(Component.text("         "))
                            .append(Component.text(CANCEL_BUTTON).hoverEvent(HoverEvent.showText(Component.text(CANCEL_BUTTON))))
            );
            return;
        }
        if (args.length != 3) {
            return;
        }
        if (!"confirm".equals(args[2])) {
            sender.sendMessage(KICK_PLAYER_COMMAND_FORMAT);
            return;
        }
        String playerName = args[1].trim();
        TownPlayerDO playerParam = new TownPlayerDO();
        playerParam.setName(playerName);
        List<TownPlayerDO> townPlayerList = TownPlayerManage.selectList(playerParam);
        OfflinePlayer playerInGame = PlayerTools.getOfflineOrOnlinePlayerByName(playerName);
        String townName = "";
        boolean success = false;
        if (!townPlayerList.isEmpty()) {
            TownPlayerDO townPlayer = townPlayerList.get(0);
            if (townPlayer.getIdentity() == PlayerTownIdentity.GENSHIN) {
                sender.sendMessage(String.format(PLAYER_NO_HAVE_TOWN, playerName));
                return;
            }
            townName = TownManage.selectByUuid(townPlayer.getTownUuid()).getName();
            townPlayer.setTownUuid("-");
            townPlayer.setIdentity(PlayerTownIdentity.GENSHIN);
            TownPlayerManage.updateByUuid(townPlayer);
            // 权限部分
            LuckPermsTown.kickPlayerFromTown(Collections.singletonList(playerName));
            success = true;
        } else {
            sender.sendMessage(PLAYER_NOT_FOUND);
        }
        if (success) {
            sender.sendMessage(String.format(KICK_PLAYER_SUCCESS, playerName));
            if (playerInGame.isOnline()) {
                ((Player) playerInGame).sendMessage(String.format(KICK_PLAYER_SUCCESS_SEND_PLAYER, townName));
            }
        }
    }
}
